package com.ruoyi.job.task;


import com.ruoyi.common.core.constant.RabbitMqConstants;
import com.ruoyi.job.mapper.SysJobMapper;
import com.ruoyi.job.service.ISysJobService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.utils.StringUtils;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    public void reTrySendMail() {



        Set<String> keys = redisTemplate.keys(RabbitMqConstants.CORRELATION_ID + "*");
        keys.forEach(msgId -> {
            msgId = msgId.split(":")[1];
            RedisAtomicInteger redisAtomicInteger = new RedisAtomicInteger(RabbitMqConstants.MAIL_SEND_RETRY_COUNT + msgId, redisTemplate.getConnectionFactory());
            int currentCount = redisAtomicInteger.getAndIncrement();
            //设置个自动过期时间确保不会占用内存
            redisAtomicInteger.expire(3, TimeUnit.MINUTES);
            String userJSON = (String) redisTemplate.opsForValue().get(RabbitMqConstants.CORRELATION_ID + msgId);
            if (currentCount > 2) {
                //重试三次失败就删除计数缓存 发送到失败队列
                redisTemplate.delete(RabbitMqConstants.MAIL_SEND_RETRY_COUNT + msgId);
                String finalMsgId = msgId;
                rabbitTemplate.convertAndSend(RabbitMqConstants.FAILED_EXCHANGE, RabbitMqConstants.FAILED_DELIVER_ROUTING_KEY, true, new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setHeader(RabbitMqConstants.MESSAGE_CORRELATION, finalMsgId);
                        return message;
                    }
                });
            }
            String finalMsgId1 = msgId;
            rabbitTemplate.convertAndSend(RabbitMqConstants.RETRY_EXCHANGE, RabbitMqConstants.RETRY_RETRY_ROUTING_KEY, userJSON, new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setHeader(RabbitMqConstants.MESSAGE_CORRELATION, finalMsgId1);
                    return message;
                }
            });
        });
    }
}
